package j.framework.tags.form;

import j.framework.core.DictItem;
import j.framework.core.FApplication;
import j.util.StringUtils;

import javax.servlet.jsp.JspException;
import java.util.List;

/**
 * 下拉列表框
 * User: jason
 * Date: 2014/12/18 11:38
 */
public class SelectTag extends BaseFormControl {

    private String code;
    private List<DictItem> items;
    private String onchange;
    private String selectedValue;
    private String theFirstItemText = "请选择...";

    @Override
    public void writeFormTagBegin(FormTagWriter writer) throws JspException {
        writer.beginTag("select");
    }

    @Override
    public void writeFormTagProperties(FormTagWriter writer) throws JspException {
        writer.writeFormTagProperty("onchange",onchange);
    }

    @Override
    public void endBeginTag(FormTagWriter writer) throws JspException {
        writer.writeSomething(">");
        //加载列表项
        if(StringUtils.isNullOrEmpty(code) &&
                items == null){
            throw new RuntimeException("SelectTag 必须指定code或items其中一项，若都指定，则以items为主！");
        }
        List<DictItem> list = this.items;
        if(null == list){
            list = FApplication.getInstance().getDictItems(code);
        }
        final StringBuilder buff = new StringBuilder();
        if(StringUtils.isNotEmpty(theFirstItemText)){
            buff.append("<option value=\"\">").append(theFirstItemText).append("</option>");
        }
        String selectedValue = this.selectedValue;
        if(null!=list && list.size()>0){
            if(selectedValue == null){
                Object o = getFormDataValue();
                if(null!=o){
                    selectedValue = o.toString();
                }
            }
            String _code;
            for(DictItem item : list){
                if (item.isDisabled()) {
                    continue;
                }
                _code = item.getCode();
                if (StringUtils.isNotEmpty(item.getValue())) {
                    _code = item.getValue();
                }else if(StringUtils.isNotEmpty(_code)){
                    if(_code.contains("_")){
                        _code = _code.substring(_code.indexOf("_")+1,_code.length());
                    }
                }
                buff.append("<option value=\"").append(_code).append("\"");
                if (null != selectedValue && selectedValue.equals(_code)) {
                    buff.append(" selected=\"selected\"");
                }
                buff.append(">");
                buff.append(item.getName()).append("</option>");
            }
        }
        writer.writeSomething(buff.toString());
        writer.closeTag("select");
    }

    @Override
    public String getCssClassName() {
        return null;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setItems(List<DictItem> items) {
        this.items = items;
    }

    public void setOnchange(String onchange) {
        this.onchange = onchange;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    public void setTheFirstItemText(String theFirstItemText) {
        this.theFirstItemText = theFirstItemText;
    }
}
